home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1994 November / macformat-018.iso / Utility Spectacular / Developer / macgambit-20-compiler-src-p1 / Runtime (.c & .h) / os.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-07-26  |  3.6 KB  |  173 lines  |  [TEXT/KAHL]

  1. /* Operating system specific stuff */
  2.  
  3.  
  4. #ifndef THINK_C
  5.  
  6. #include "params.h"
  7. #include "gambit.h"
  8. #include "os.h"
  9. #include "mem.h"
  10. #include "strings.h"
  11. #include "opcodes.h"
  12.  
  13. OS_FILE os_stdin, os_stdout, os_stderr;
  14. long os_M68020 = 1, os_M68881 = 1;  /* Assume M68020/30 processor and M68881 */
  15. char *os_err = "";
  16.  
  17. #endif
  18.  
  19. /*---------------------------------------------------------------------------*/
  20.  
  21. /* This section includes machine dependent code. */
  22.  
  23. #ifdef unix
  24. #include "os_unix.c"
  25. #else
  26. #ifdef mac
  27. #ifndef THINK_C
  28. /* ThinkC project includes os-mac.c  6Jul92  e  */
  29. #include "os_mac.c"
  30. #endif
  31. #else
  32. #include "os_ansi.c"
  33. #endif
  34. #endif
  35.  
  36.  
  37. /*---------------------------------------------------------------------------*/
  38.  
  39. /*
  40.   The code that is fairly machine independent is included here.  Machine
  41.   dependent actions are signaled by calls to `internal' procedures.
  42. */
  43.  
  44.  
  45. #define MAX_STDOUT_BUF_LEN 256
  46.  
  47. static char stdout_buf[MAX_STDOUT_BUF_LEN];
  48. static long stdout_buf_len = 0;
  49. static char digits[] = "0123456789abcdef";
  50.  
  51.  
  52. static void print_char( f, c )
  53. OS_FILE f;
  54. char c;
  55. { if (f == os_stdout)
  56.   { stdout_buf[stdout_buf_len++] = c;
  57.     if (c == '\n')
  58.     { os_file_write( os_stdout, stdout_buf, stdout_buf_len );
  59.       stdout_buf_len = 0;
  60.     }
  61.     else if (stdout_buf_len == MAX_STDOUT_BUF_LEN) stdout_buf_len--;
  62.   }
  63.   else
  64.   { char buf[1];
  65.     buf[0] = c;
  66.     os_file_write( f, buf, 1L );
  67.   }
  68. }
  69.  
  70.  
  71. static void print_str( f, str )
  72. OS_FILE f;
  73. char *str;
  74. { if (f == os_stdout)
  75.     while (*str != '\0')
  76.     { stdout_buf[stdout_buf_len++] = *str;
  77.       if (*str++ == '\n')
  78.       { os_file_write( os_stdout, stdout_buf, stdout_buf_len );
  79.         stdout_buf_len = 0;
  80.       }
  81.       else if (stdout_buf_len == MAX_STDOUT_BUF_LEN) stdout_buf_len--;
  82.     }
  83.   else
  84.   { long len = 0;
  85.     while (str[len] != '\0') len++;
  86.     os_file_write( f, str, len );
  87.   }
  88. }
  89.  
  90.  
  91. static void print_int( f, n, radix )
  92. OS_FILE f;
  93. long n;
  94. long radix;
  95. { char buf[12];
  96.   char *p = buf;
  97.   long x, m;
  98.   if (n < 0) { *p++ = '-'; n = -n; }
  99.   x = n; m = 1;
  100.   while (x >= radix) { x = x / radix; m = m * radix; }
  101.   while (m > 0)
  102.   { long d = n / m;
  103.     *p++ = digits[d];
  104.     n = n - d * m;
  105.     m = m / radix;
  106.   }
  107.   *p++ = '\0';
  108.   print_str( f, buf );
  109. }
  110.  
  111.  
  112. void os_file_printf( f, str, val )
  113. OS_FILE f;
  114. char *str;
  115. long val;
  116. { char *p = str;
  117.   while ((*p != '\0') && (*p != '%')) print_char( f, *(p++) );
  118.   if (*(p++) == '%')
  119.   { switch (*p++)
  120.     { case '%': print_char( f, '%' );         break;
  121.       case 's': print_str( f, (char *)val );  break;
  122.       case 'd': print_int( f, val, 10L );     break;
  123.       case 'x': print_int( f, val, 16L );     break;
  124.     }
  125.     print_str( f, p );
  126.   }
  127. }
  128.  
  129.  
  130. void os_warn( msg, val )
  131. char *msg;
  132. long val;
  133. { os_file_printf( os_stdout, msg, val );
  134. }
  135.  
  136.  
  137. void os_notify_gc_begin( id, report )
  138. long id;
  139. long report;
  140. { os_notify_gc_begin_internal();
  141.   if (report)
  142.     os_warn( "[GC start on processor %d]\n", id );
  143. }
  144.  
  145.  
  146. void os_notify_gc_end( id, heap_mid, heap_bot, free_bot, free_top, report )
  147. long id;
  148. char *heap_mid, *heap_bot, *free_bot, *free_top;
  149. long report;
  150. { if (report)
  151.   { long total = heap_mid - heap_bot;
  152.     long free = free_top - free_bot;
  153.     long usage = total - free;
  154.     long usage_ratio_times_1000 = usage/(total/1000);
  155.     os_warn( "[GC end on processor %d", id );
  156.     os_warn( ", heap usage = %dK", usage/K );
  157.     os_warn( " (%d", usage_ratio_times_1000/10 );
  158.     os_warn( ".%d", usage_ratio_times_1000%10 );
  159.     os_warn( "%%)]\n", 0L );
  160.   }
  161.   os_notify_gc_end_internal();
  162. }
  163.  
  164.  
  165. int main( argc, argv, envp )
  166. int argc;
  167. char *argv[], *envp[];
  168. { return main_internal( argc, argv, envp );
  169. }
  170.  
  171.  
  172. /*---------------------------------------------------------------------------*/
  173.